"""
TextFileKit creates text file objects that can be manipulated accordingly.
"""
import fimero.core.disk.PathKit as PathKit
import fimero.core.disk.FileKit as FileKit
import fimero.core.util.LogKit as LogKit

LOG = LogKit.get_logger(__name__)

class TTextFile(FileKit.TFile):
    """
    TTextFile represents a text file.
    """
    
    def __init__(self, path):
        """Initialize the text file instance with specified path."""
        FileKit.TFile.__init__(self, path)
        
    def read_text(self):
        """Read all the file's text in one go."""
        handle = self.get_handle(FileKit.MODE_TEXT)
        try:
            return handle.read()
        finally:
            handle.close()
            
    def write_text(self, text):
        """Write text (overwriting any existing data)"""
        handle = self.get_handle(FileKit.MODE_TEXT_WRITE)
        try:
            handle.write(text)
        finally:
            handle.close()
            
    def readlines(self):
        """Return lines of text as a list."""
        return self.get_handle(FileKit.MODE_TEXT).readlines()
    
    def lines(self):
        """Returns total number of lines."""
        return len(self.get_handle(FileKit.MODE_TEXT).readlines())
    
def make(path):
    """Make a new text file instance."""
    path = PathKit.make(path)
    return TTextFile(path)
    